# -*- coding: utf-8 -*-
# 2022/4/15 13:06
# Author :jiangdeping
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from page.personaldomain import personaldomain
from page.base_page import BasePage
from selenium.webdriver import ActionChains
import time
class WorkSpace(BasePage):
    def getloginname(self):
        self.wait_for_click((By.CSS_SELECTOR, ".username-hide-on-mobile"), 10)
        element=self._driver.find_element(By.CSS_SELECTOR,".username-hide-on-mobile")
        loginname=element.text
        return loginname
    def getuntreatedtask(self):
        self.wait_for_click((By.CSS_SELECTOR, ".icon-calendar"), 10)
        number = self._driver.find_element(By.CSS_SELECTOR, ".badge-primary").text
        # ActionChains(self._driver).move_to_element(element)
        return number
    def getalltask(self):
        element = self._driver.find_element(By.CSS_SELECTOR, '.icon-calendar')
        ActionChains(self._driver).move_to_element(element).perform()
        time.sleep(2)
        elements=self._driver.find_elements(By.CSS_SELECTOR,'.desc')
        # for i  in elements:
        #     print(i.text)
        self.wait_for_click((By.CSS_SELECTOR,"#task"),10)
        # 获取标签中的文本内容
        element= self._driver.find_element(By.CSS_SELECTOR, "#task>ul>li>a")
        from selenium.webdriver.common.keys import Keys
        #a标签：查看所有点击跳转
        element.send_keys(Keys.ENTER)
        result=element.get_attribute("textContent")
        href=element.get_attribute("href")
        print(f"href if {href}")
        # showContent('/bpe/task/todoList')
        time.sleep(2)
        print(f'element is {result}')
        ActionChains(self._driver).move_to_element(element).perform()
        elements = self._driver.find_elements(By.CSS_SELECTOR, '.desc')
        for i in elements[0:5]:
            ActionChains(self._driver).move_to_element(element).perform()
            self.goto_task_detail(i)
    def goto_task_detail(self,locator:WebElement):
            planNo=locator.text
            print(f"planNo is: {planNo}")
            if len(planNo)>0:
                locator.click()


    def fault_report(self):
        self._driver.switch_to_frame('content_iframe')
        time.sleep(2)
        totalcount=self._driver.find_element(By.CSS_SELECTOR,'#oldfaultCount').text.split(':')[1]
        faultcount=self._driver.find_element(By.CSS_SELECTOR,'#faultCount').text.split(':')[1]
        self._driver.switch_to_default_content()
        return int(totalcount),int(faultcount)
        #考虑装饰器

    def getMenu(self):
        #https://blog.huati365.com/9b30fd46acb1487d
        #https://meiwencun.com/python_selenium_zhong_dui_xiang_353/
        dict={}

        elements=self._driver.find_elements(By.CSS_SELECTOR,'.page-sidebar-menu>li')
        for element in elements:
            oneelement=element.find_element(By.CSS_SELECTOR,'.uppercaseTitle')
            oneelementtitle=oneelement.text
            print(f"oneelement value is {oneelementtitle}")
            oneelement.click()
            twoelements=element.find_elements(By.CSS_SELECTOR,'.sub-menu .nav-toggle')

            twodicts={}
            twovars=[]
            onewars=[]
            for twoelement in twoelements:
                twoparentelement = twoelement.find_element(By.XPATH, './..')
                print(f"twoelement value is {twoelementtitle}")
                self._driver.implicitly_wait(0)  # 找不到元素等待时间
                # title.click()
                # otherelements=parentelement.find_elements(By.CSS_SELECTOR,'.thirdItem .title')
                try:
                    flag=twoelement.find_element(By.CSS_SELECTOR,'.arrow')
                    # self._driver.implicitly_wait(5)
                    if flag:
                        title.click()
                        threeelements = twoparentelement.find_elements(By.CSS_SELECTOR,'.thirdItem .title')
                        threevars=[]
                        for i in threeelements:
                            self.movetoelement(i)
                            threeelementtiele=i.text
                            threevars.append(threeelementtiele)
                            print(f"threeelement value is {threeelementtiele}")
                        twodicts[twoelementtitle]=threevars
                        onewars.append(twodicts)

                except:
                    onewars.append(twoelementtitle)
            print(f"one {oneelementtitle}")
            print(f"wars {onewars}")
            dict[oneelementtitle]=onewars

        print(dict)
        #获取一级菜单   $(".page-sidebar-menu>li:nth-child(1)>a>span.uppercaseTitle")
        #获取二级菜单   $(".page-sidebar-menu>li:nth-child(1)>ul>li>a>span.title")
        #获取二级标识   $(".page-sidebar-menu>li:nth-child(1)>ul>li>a>span.arrow")
        #获取三级菜单   $(".page-sidebar-menu>li:nth-child(1)>ul>li>ul>li>a>span.title")


    def getMenubak(self):

        elements=self._driver.find_elements(By.CSS_SELECTOR,'.page-sidebar-menu>li')
        print(f'elements 共有 {len(elements)}')
        for i in range(11,len(elements)+1):
            oneelement=self._driver.find_element(By.CSS_SELECTOR,'.page-sidebar-menu>li:nth-child(%s)>a>span.uppercaseTitle'%i)
            twoelements = self._driver.find_elements(By.CSS_SELECTOR,'.page-sidebar-menu>li:nth-child(%s)>ul>li>a>span.title' % i)
            threeelements = self._driver.find_elements(By.CSS_SELECTOR,'.page-sidebar-menu>li:nth-child(%s)>ul>li>ul>li>a>span.title' % i)
            print(f"oneelement value is {oneelement.text}")
            self.new_click(oneelement)
            if len(twoelements) > 1:
                print(f"twoelement len is {len(twoelements)}")
                for twoelement in twoelements:
                    print(f"twoelement value is {twoelement.text}")
                    self.new_click(twoelement)
                    if len(threeelements) > 1:
                        self.getthreeelement(threeelements)
            else:
                twoelement=twoelements[0]
                self.new_click(twoelement)
                print(f"else twoelement value is {twoelement.text}")
                self.getthreeelement(threeelements)


    def newclick(self,location :WebElement):
        try:
            location.click()
            print("location.click")
        except:
            self.movetoelement(location)
            print("movetoelement location.click")
            location.click()
    def movetoelement(self,location):
        target=location
        js="arguments[0].scrollIntoView(true);"
        self.execute_script(js,target)
    def execute_script(self,*args):
        self._driver.execute_script(*args)
    def  into_personaldomain(self):
        return personaldomain(self._driver)
